package com.ruoyi.zyx.controller;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zyx.domain.ZyxCost;
import com.ruoyi.zyx.service.IZyxCostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用明细
Controller
 *
 * @author lhs
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/zyx/cost")
public class ZyxCostController extends BaseController
{
    @Autowired
    private IZyxCostService zyxCostService;

    /**
     * 查询费用明细列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyxCost zyxCost)
    {
        startPage();
        List<ZyxCost> list = zyxCostService.selectZyxCostList(zyxCost);
        return getDataTable(list);
    }

    @GetMapping("/getNoPageList")
    public AjaxResult getNoPageList(ZyxCost zyxCost)
    {
        AjaxResult ajaxResult=AjaxResult.success();
        List<ZyxCost> list = zyxCostService.selectZyxCostList(zyxCost);
//        BigDecimal sum =0.00;
        BigDecimal sum = new BigDecimal("0.0");
        for (ZyxCost cost : list) {
            sum=sum.add(cost.getCost());
        }
        ajaxResult.put("list",list);
        ajaxResult.put("sum",sum);

        return ajaxResult;
    }


    /**
     * 导出费用明细列表
     */
    @PreAuthorize("@ss.hasPermi('zyx:cost:export')")
    @Log(title = "费用明细 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZyxCost zyxCost)
    {
        List<ZyxCost> list = zyxCostService.selectZyxCostList(zyxCost);
        ExcelUtil<ZyxCost> util = new ExcelUtil<ZyxCost>(ZyxCost.class);
        return util.exportExcel(list, "费用明细 数据");
    }

    /**
     * 获取费用明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('zyx:cost:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zyxCostService.selectZyxCostById(id));
    }

    /**
     * 新增费用明细

     */
    @PreAuthorize("@ss.hasPermi('zyx:cost:add')")
    @Log(title = "费用明细 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyxCost zyxCost)
    {
        return toAjax(zyxCostService.insertZyxCost(zyxCost));
    }

    /**
     * 修改费用明细

     */
    @PreAuthorize("@ss.hasPermi('zyx:cost:edit')")
    @Log(title = "费用明细 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyxCost zyxCost)
    {
        return toAjax(zyxCostService.updateZyxCost(zyxCost));
    }

    /**
     * 删除费用明细

     */
    @PreAuthorize("@ss.hasPermi('zyx:cost:remove')")
    @Log(title = "费用明细 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zyxCostService.deleteZyxCostByIds(ids));
    }
}
